package com.blog.controller;

import com.blog.exception.ExceptionEnum;
import com.blog.pojo.Comment;
import com.blog.pojo.CommentWithUsernameAndImageVO;
import com.blog.pojo.PageBean;
import com.blog.pojo.ResultResponse;
import com.blog.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/comments")
@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;

    // 分页展示所有评论
    @GetMapping("/all")
    public ResultResponse listAll(@RequestParam(defaultValue = "1") Integer page) {
        log.info("分页展示所有评论");
        Integer pageSize = 10;
        PageBean pageBean = commentService.listAll(page, pageSize);
        return ResultResponse.success(pageBean);
    }

    // 分页展示当前用户收到的所有评论
    @GetMapping("/currentUser")
    public ResultResponse listCurrentUser(@RequestParam(defaultValue = "1") Integer page) {
        log.info("分页展示当前用户收到的所有评论");
        Integer pageSize = 10;
        PageBean pageBean = commentService.listCurrentUser(page, pageSize);
        return  ResultResponse.success(pageBean);
    }

    // 展示单篇文章下所有的评论
    @GetMapping("/article/{id}")
    public ResultResponse listByArticleId(@PathVariable Integer id) {
        log.info("展示一篇文章下所有评论，文章id: {}", id);
        List<CommentWithUsernameAndImageVO> commentList = commentService.listByArticleId(id);
        return ResultResponse.success(commentList);
    }

    // 发布评论
    @PostMapping
    public ResultResponse add(@RequestBody Comment comment) {
        log.info("发布评论");
        if(comment.getContent() == null) {
            return ResultResponse.error(ExceptionEnum.EMPTY_CONTENT);
        }
        commentService.add(comment);
        return ResultResponse.success();
    }

    // 根据评论id删除评论
    @DeleteMapping("/{id}")
    public ResultResponse deleteById(@PathVariable Integer id) {
        log.info("删除评论，评论id: {}", id);
        commentService.deleteById(id);
        return ResultResponse.success();
    }
}
